package com.onstar.cn.common.util;

import android.content.Context;

import com.onstar.cn.common.log.LogManager;

public class EncryptUtil {
	private static LogManager logger = LogManager.getInstance();

	// 加密
	public static String encryptBase64(Context context, int resId) {
		String base64key = "ABYqI1NoYW5nSGFpT25TdGFyJCZAISMq";
		String key = new String(Base64.decodeToString(base64key));
		try {
			return SimpleCrypto.encrypt(key, context.getResources().getString(resId));
		} catch (Exception e) {
			logger.e(e.getMessage(), e);
			return context.getResources().getString(resId);
		}
	}

	// 解密
	public static String decryptBase64(Context context, int resId) {
		String base64key = "ABYqI1NoYW5nSGFpT25TdGFyJCZAISMq";
		String key = new String(Base64.decodeToString(base64key));
		try {
			return SimpleCrypto.decrypt(key, context.getResources().getString(resId));
		} catch (Exception e) {
			logger.e(e.getMessage(), e);
			return context.getResources().getString(resId);
		}
	}
	
	// 加密
		public static String encryptBase64(Context context, String str) {
			String base64key = "ABYqI1NoYW5nSGFpT25TdGFyJCZAISMq";
			String key = new String(Base64.decodeToString(base64key));
			try {
				return SimpleCrypto.encrypt(key, str);
			} catch (Exception e) {
				logger.e(e.getMessage(), e);
				return str;
			}
		}

		// 解密
		public static String decryptBase64(Context context, String str) {
			String base64key = "ABYqI1NoYW5nSGFpT25TdGFyJCZAISMq";
			String key = new String(Base64.decodeToString(base64key));
			try {
				return SimpleCrypto.decrypt(key, str);
			} catch (Exception e) {
				logger.e(e.getMessage(), e);
				return str;
			}
		}

	// 加密
	public static String encrypt(Context context, String str) {
		if (str == null) {
			str = "";
		}
		String encryptStr = null;
		String key = "*#" + new DeviceUtil(context).getDeviceID() + "$&@!#*";
		try {
			encryptStr = SimpleCrypto.encrypt(key, str);
		} catch (Exception e) {
			e.printStackTrace();
			logger.e(e.getMessage(), e);
			encryptStr = str;
		}
		return encryptStr;
	}

	// 解密
	public static String decrypt(Context context, String str, String def) {
		String key = "*#" + new DeviceUtil(context).getDeviceID() + "$&@!#*";
		if (str == null) {
			return null;
		} else if (str.equals(def)) {
			return def;
		} else {
			try {
				return SimpleCrypto.decrypt(key, str);
			} catch (Exception e) {
				logger.e(e.getMessage(), e);
				return def;
			}
		}
	}
}
